package org.compiere.process;

import org.adempiere.apps.graph.BSCCardPanelNew;
import org.adempiere.apps.graph.BSCViewBarChart;
import org.adempiere.apps.graph.BSCViewDashboard1;
import org.compiere.apps.AEnv;
import org.compiere.apps.form.FormFrame;
import org.compiere.model.MBSCCard;
import org.compiere.model.MBSCCardLine;
import org.compiere.model.MBSCCardLineLink;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class BSCShowDashboard extends SvrProcess {

	private MBSCCard card = null;
	
	
	@Override
	protected void prepare() {
		initParameters();

	}

	@Override
	protected String doIt() throws Exception {
		if (card != null) {
			FormFrame myWindow = new FormFrame(Env.getWindow(0).getGraphicsConfiguration());
			if (myWindow.openForm(getBSCViewDashboard1())) {
				BSCViewDashboard1 view = (BSCViewDashboard1) myWindow.getFormPanel();
				view.setCard(card);
				AEnv.addToWindowManager(myWindow);
				AEnv.showCenterScreen(myWindow);
				myWindow.setVisible(true);
				myWindow.setFocusableWindowState(true);
				myWindow.toFront();
			}
		}
		return null;
	}


	private int getBSCViewDashboard1() {
		int result = 0;
		String sql = "SELECT AD_Form_ID FROM AD_Form WHERE Name like 'BSC_Dashboard1'";
		result = DB.getSQLValue(null, sql);
		return result;
	}

	private void initParameters() {
		int BSC_CardLine_Link_ID  = 0;
		for(ProcessInfoParameter param: getParameter()) {
			if (param.getParameterName().equals("BSC_CardLine_Link_ID")) {
				BSC_CardLine_Link_ID = Integer.parseInt(param.getParameter().toString());
			}
		}
		if (BSC_CardLine_Link_ID > 0) {
			MBSCCardLineLink cardLineLink = new MBSCCardLineLink(Env.getCtx(),BSC_CardLine_Link_ID,null);
			int BSC_CardLine_ID = (cardLineLink != null ? cardLineLink.getlink_BSC_CardLine_ID() : 0);
			if (BSC_CardLine_ID > 0) {
				MBSCCardLine cardLine = new MBSCCardLine(Env.getCtx(),BSC_CardLine_ID,null);
				if (cardLine != null) {
					card = cardLine.getCard(); 
				}
			}
		}
	}
}
